package com.cskaoyan.controller.mallController;

import com.cskaoyan.exception.ShipSnException;
import com.cskaoyan.model.bo.PageBo;
import com.cskaoyan.model.bo.mall.OrderRefundBo;
import com.cskaoyan.model.bo.mall.OrderShipBo;
import com.cskaoyan.model.vo.BaseRespVo;
import com.cskaoyan.model.vo.mall.OrderDetailVo;
import com.cskaoyan.model.vo.mall.OrderVo;
import com.cskaoyan.service.mall.MallAfterOrderService;
import com.cskaoyan.utils.StringUtils;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/admin/order")
@RequiresPermissions("admin:order:*")
public class MallOrderController {


    private ObjectMapper objectMapper = new ObjectMapper();

    {
        objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
    }

    @Autowired
    MallAfterOrderService mallAfterOrderService;

    /**
     * @param pageBo           分页
     * @param orderSn          订单编号
     * @param userId           用户id
     * @param orderStatusArray 好多个选定的状态码
     * @return BaseRespVo
     */
    //都是mall里面的,做个小分支
    @RequestMapping("/list")
    public BaseRespVo getOrderList(PageBo pageBo, String orderSn, String userId, String[] orderStatusArray) {
        Integer id = null;
        if (StringUtils.isNotEmpty(userId)) {
            try {
                id = Integer.parseInt(userId);
            } catch (NumberFormatException e) {
                return BaseRespVo.error(402, "userId必须为数字");
            }
        }

        List<String> stringList = new ArrayList<>();
        if (orderStatusArray != null && orderStatusArray.length > 1) {
            stringList = Arrays.asList(orderStatusArray);
        } else {
            stringList = null;
        }
        OrderVo orderVo = mallAfterOrderService.getOrderList(pageBo, orderSn, id, stringList);

        return BaseRespVo.ok(orderVo);
    }


    @GetMapping("/detail")
    public BaseRespVo orderDetail(Integer id) {

        OrderDetailVo orderDetailVo = mallAfterOrderService.orderDetail(id);

        return BaseRespVo.ok(orderDetailVo);
    }

    @PostMapping("/refund")
    public BaseRespVo orderRefund(@RequestBody OrderRefundBo orderRefundBo) {

        boolean b = mallAfterOrderService.orderRefund(orderRefundBo.getOrderId());
        if (b) {
            return BaseRespVo.ok();
        }
        return BaseRespVo.error(402, "退款失败");
    }

    @PostMapping("/ship")
    public BaseRespVo orderShip(@RequestBody OrderShipBo orderShipBo) throws ShipSnException {

        String s=orderShipBo.getShipSn();
        String regex="^\\d+$";
        if(!s.matches(regex)){
            System.out.println("匹配");
            throw new ShipSnException("只能输入数字,别填乱八七糟的",s);
        }
        boolean b = mallAfterOrderService.orderShip(orderShipBo.getOrderId(),orderShipBo.getShipChannel(),orderShipBo.getShipSn());
        if (b) {
            return BaseRespVo.ok();
        } else {
            return BaseRespVo.error(402, "发货失败");
        }
    }
}
